<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Template extends Controller {
	
	//网页标题
	protected $title;
	
	//面包屑导航
	protected $navigate;
	
	//主体内容
	protected $content;
	
	//是否ajax
	public $_is_ajax;
	
	//ajax对象
	public $ajax;
	
	//数据库实体
	public $_conn;
	
	//服务器端缓存
	public $_session;
	
	//文件缓存
	public $_cache;
	
	//配置容器
	public $_config;
	
	//分页容器
	public $pagination;
	
	//是否自动显示
	protected $auto_render = TRUE;
	
	//用户变量
	public $user;
	
	//$_POST数组
	public $post;
	
	//$_GET数组
	public $get;
	
	//前一页变量
	public $referer = '/';
	
	public function before(){
		parent::before();
		
		$this->unit = lib_unittest::start(false);
		
		defined('INCLUDED') or include_once Kohana::find_file('classes','include');
		
		//数据库连接
		//MysqlDB::instance(get_object_vars(project::config('conndb')));
		$this->_conn = null;
		
		//session
		$this->_session = Session::instance();
        //缓存
        $this->_cache = Cache::instance();
        //配置文件
        $this->_config = project::config('common');
        //表单数据
        $this->post = lib_security::killinject($this->request->post());
        //请求数据
        $this->get = lib_security::killinject($_GET);
        //前一页
        $this->referer = lib_url::HTTP_REFERER();
        
        //账户初始化
        /*$this->user = lib_user::instance();
		if (($this->user->username == 'admin') && ($this->user->power == 1) && ($this->user->parentId == 0)){
        	lib_url::redirect('admin');
        }*/
        
		if ($this->request->is_ajax())
		{
			$this->_is_ajax = TRUE;
			$this->ajax = new lib_ajax();
		}else{
			$this->_is_ajax = FALSE;
			$this->ajax = new lib_void();
		}
		
        if ($this->_is_ajax === TRUE)
		{
			// Turn off auto-rendering
			$this->auto_render = FALSE;
			
			// Send headers for a json response
			$this->request->headers ['Cache-Control'] = 'no-cache, must-revalidate';
			$this->request->headers ['Expires'] = 'Sun, 30 Jul 1989 19:30:00 GMT';
		}
		
		if (TRUE === $this->auto_render){     
			//初始化模板视图
			$this->title = '首页';
			$this->navigate = new lib_navigate();
			$this->navigate->add('首页','/');
			$this->template = new lib_template();
            
            //样式表直显
            $this->template->style('media/css/common.css');
            
            //js直显
            $this->template->endscript('media/js/default.js');
            
            //样式表
            $this->template->css(array());
            
            //js
            $this->template->js(array(
            	'j/jquery-1.7.2.min.js',
            	'j/common.js'
            ));
            $this->template->endjs(array());
		}
	}
	
	public function after(){
		if (TRUE === $this->auto_render){
			//标题
			$this->template->title = $this->title.$this->_config->title;
			//导航
			$this->template->navigate = $this->navigate;
			//主体内容
			$this->template->main(View::factory('template/main'));
            $this->template->head(View::factory('template/head'));
			$this->template->foot(View::factory('template/foot'));
			$this->template->extra(View::factory('template/extra'));
			$this->template->content($this->content);
			
			View::set_global('base_url',lib_url::base());
			View::set_global('location_main',$this->request->controller());
			View::set_global('location_sub',$this->request->action());
			View::bind_global('object',$this);
	        View::bind_global('referer',$this->referer);
	        View::bind_global('pagination',$this->pagination);
	        //提示信息
	        $message = lib_message::messageShow('front');
	        View::bind_global('message',$message);
	        
			$this->response->body($this->template->render());
		}elseif (TRUE === $this->_is_ajax)
		{
			$this->ajax->exec();
		}
		
		$this->unit->end();
		
		parent::after();
	}
	
	public function action_index()
	{
		lib_url::redirect($this->referer);
		exit;
	}

} // End Welcome
